﻿using System;
using System.Collections;
using System.Configuration;
using System.Data;
using System.Linq;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
using System.Xml.Linq;

namespace pcsse.MasterPages
{
    public partial class WebForm5 : System.Web.UI.Page
    {
        protected DataTable dt;
        protected DataTable dl_dt;
        const int Page_size = 20;
        protected int Page_num;
        private void DDL_bind()
        {
            dt = db_news.getDBhandle().GetDataTableBySQL("SELECT * FROM T_NEWS,T_CATEGORY WHERE T_NEWS.CATEGORY_ID=T_CATEGORY.CATEGORY_ID ORDER BY T_NEWS.DATE DESC");
            DataList1.DataSource = dt;
            DataList1.DataBind();
            //=====================================================
            dl_dt = db_news.getDBhandle().GetDataTableBySQL("SELECT * FROM T_CATEGORY");
            DropDownList1.DataSource = dl_dt;
            DropDownList1.DataValueField = dl_dt.Columns[0].ColumnName;
            DropDownList1.DataTextField = dl_dt.Columns[1].ColumnName;
            DropDownList1.DataBind();
        }
        protected void DL_bind(int page_index, DataTable dtTb)
        {
            //int cur_page_num;
            PagedDataSource ps = new PagedDataSource();
            ps.DataSource = dtTb.DefaultView;
            ps.AllowPaging = true;
            ps.PageSize = Page_size;
            ps.CurrentPageIndex = page_index;
            LbPageCount.Text = " 共" + ps.PageCount.ToString() + "页";
            DataList1.DataSource = ps;
            DataList1.DataBind();
            if (!ps.IsFirstPage)
            {
                LBPrev.Enabled = true;
            }
            else
            {
                LBPrev.Enabled = false;
            }
            if (!ps.IsLastPage)
            {
                LBNext.Enabled = true;
            }
            else
            {
                LBNext.Enabled = false;
            }
        }
        private void DL_SelectByCategory(string CategoryID)
        {
            dt = db_news.getDBhandle().GetNewsByCategoryID(CategoryID);
            DataList1.DataSource = dt;
            DataList1.DataBind();
        }
        protected void Page_Load(object sender, EventArgs e)
        {
            if (Convert.ToInt32(Session["Priority"]) < 4) Response.Redirect("..\\");
            if (Request.QueryString["page"] == null)
            {
                Page_num = 0;
            }
            else
            {
                Page_num = int.Parse(Request.QueryString["page"]);
            }
            if (!Page.IsPostBack)
            {
                DDL_bind();
                if (Request.QueryString["cateid"] == null)
                {
                    DL_bind(Page_num, db_news.getDBhandle().GetNewsByCategoryID(DropDownList1.SelectedValue));
                }
                else
                {
                    DL_bind(Page_num, db_news.getDBhandle().GetNewsByCategoryID(Request.QueryString["cateid"]));
                    DropDownList1.SelectedValue = Request.QueryString["cateid"];
                }

                LbPage.Text = "第" + (Page_num + 1).ToString() + "页";
            }
        }

        protected void BtnSave_Click(object sender, EventArgs e)
        {

        }

        protected void BtnCancel_Click(object sender, EventArgs e)
        {

        }

        protected void DropDownList1_SelectedIndexChanged(object sender, EventArgs e)
        {

        }
        public void BtnCommand(object source, DataListCommandEventArgs e)
        {
            switch (e.CommandName)
            {
                case "BtnDel":
                    db_news.getDBhandle().DelNewsById(DataList1.DataKeys[e.Item.ItemIndex].ToString());
                    break;
            }
            //DL_bind(Page_num, db_news.getDBhandle().GetNewsByCategoryID(DropDownList1.SelectedValue));
            Response.Redirect("news_manager.aspx");
        }

        protected void Button3_Click(object sender, EventArgs e)
        {
            //Page_num = 0;
            //DL_bind(Page_num, db_news.getDBhandle().GetNewsByCategoryID(DropDownList1.SelectedValue));
            Response.Redirect("news_manager.aspx?cateid="+DropDownList1.SelectedValue);
        }

        protected void LBPrev_Click(object sender, EventArgs e)
        {
            string urlstr = "news_manager.aspx?page=" + (Page_num - 1).ToString();
            if (Request.QueryString["cateid"] != null)
            {
                urlstr = urlstr + "&cateid=" + Request.QueryString["cateid"];
            }
            Response.Redirect(urlstr);
        }

        protected void LBNext_Click(object sender, EventArgs e)
        {
            string urlstr = "news_manager.aspx?page=" + (Page_num + 1).ToString();
            if (Request.QueryString["cateid"] != null)
            {
                urlstr = urlstr + "&cateid=" + Request.QueryString["cateid"];
            }

            Response.Redirect(urlstr);
        }
    }
}
